package com;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class DrillBoxAllocator {

    public static void main(String[] args) {
        String jsonData = "{\n" +
                "  \"macCode\": \"E001\",\n" +
                "  \"transCode\": \"dgas-ihewotru8r-t7345rw\",\n" +
                "  \"axles\": 10,\n" +
                "  \"drillArray\": [\n" +
                "    {\"tools\": \"T01\", \"drillSize\": \"3175\", \"drillCount\": 3, \"holeType\": \"1\", \"drillGrindCount\": 2},\n" +
                "    {\"tools\": \"T02\", \"drillSize\": \"0200\", \"drillCount\": 118, \"holeType\": \"1\", \"drillGrindCount\": 1},\n" +
                "    {\"tools\": \"T03\", \"drillSize\": \"0225\", \"drillCount\": 80, \"holeType\": \"1\", \"drillGrindCount\": 0},\n" +
                "    {\"tools\": \"T04\", \"drillSize\": \"0250\", \"drillCount\": 60, \"holeType\": \"1\", \"drillGrindCount\": 2},\n" +
                "    {\"tools\": \"T05\", \"drillSize\": \"0275\", \"drillCount\": 28, \"holeType\": \"1\", \"drillGrindCount\": 2},\n" +
                "    {\"tools\": \"T06\", \"drillSize\": \"0300\", \"drillCount\": 22, \"holeType\": \"1\", \"drillGrindCount\": 4},\n" +
                "    {\"tools\": \"T07\", \"drillSize\": \"0350\", \"drillCount\": 16, \"holeType\": \"1\", \"drillGrindCount\": 2},\n" +
                "    {\"tools\": \"T08\", \"drillSize\": \"0400\", \"drillCount\": 8, \"holeType\": \"1\", \"drillGrindCount\": 3},\n" +
                "    {\"tools\": \"T09\", \"drillSize\": \"0450\", \"drillCount\": 6, \"holeType\": \"2\", \"drillGrindCount\": 2},\n" +
                "    {\"tools\": \"T10\", \"drillSize\": \"0450\", \"drillCount\": 4, \"holeType\": \"2\", \"drillGrindCount\": 2},\n" +
                "    {\"tools\": \"T11\", \"drillSize\": \"0500\", \"drillCount\": 3, \"holeType\": \"2\", \"drillGrindCount\": 2},\n" +
                "    {\"tools\": \"T12\", \"drillSize\": \"0650\", \"drillCount\": 4, \"holeType\": \"1\", \"drillGrindCount\": 1},\n" +
                "    {\"tools\": \"T13\", \"drillSize\": \"5000\", \"drillCount\": 1, \"holeType\": \"1\", \"drillGrindCount\": 2},\n" +
                "    {\"tools\": \"T14\", \"drillSize\": \"6000\", \"drillCount\": 2, \"holeType\": \"1\", \"drillGrindCount\": 1}\n" +
                "  ],\n" +
                "  \"Timestamp\": \"2024-12-10 22:31:09\"\n" +
                "}";

        JSONObject jsonObject = JSONObject.parseObject(jsonData);
        JSONArray drillArray = jsonObject.getJSONArray("drillArray");

        // 主刀具盒和备用刀具盒
        List<String[][]> mainBoxes = new ArrayList<>();
        List<String[][]> spareBoxes = new ArrayList<>();

        // 初始化主刀具盒
        int toolCount = drillArray.size();
        int mainBoxCount = (toolCount + 49) / 50; // 计算需要的主刀具盒数量
        for (int i = 0; i < mainBoxCount; i++) {
            mainBoxes.add(new String[10][5]);
        }

        // 分配主刀具盒
        int currentMainBox = 0;
        int currentRow = 0;
        int currentCol = 0;
        for (int i = 0; i < drillArray.size(); i++) {
            JSONObject drill = drillArray.getJSONObject(i);
            String tool = drill.getString("tools");
            mainBoxes.get(currentMainBox)[currentRow][currentCol] = tool;

            // 更新位置
            currentRow++;
            if (currentRow == 10) {
                currentRow = 0;
                currentCol++;
                if (currentCol == 5) {
                    currentCol = 0;
                    currentMainBox++;
                }
            }
        }

        // 分配备用刀具盒
        int currentSpareBox = 0;
        currentRow = 0;
        currentCol = 0;
        for (int i = 0; i < drillArray.size(); i++) {
            JSONObject drill = drillArray.getJSONObject(i);
            String tool = drill.getString("tools");
            int drillCount = drill.getIntValue("drillCount");
            if (drillCount > 1) {
                // 剩余钻针数量
                int remainingDrills = drillCount - 1;

                while (remainingDrills > 0) {
                    // 当前列剩余空间
                    int availableRows = 10 - currentRow;

                    // 当前列能放入的数量
                    int drillsToPlace = Math.min(availableRows, remainingDrills);

                    // 放入当前列
                    for (int j = 0; j < drillsToPlace; j++) {
                        spareBoxes.get(currentSpareBox)[currentRow][currentCol] = tool;
                        currentRow++;
                        remainingDrills--;
                    }

                    // 如果当前列已满，移动到下一列
                    if (currentRow == 10) {
                        currentRow = 0;
                        currentCol++;
                        if (currentCol == 5) {
                            currentCol = 0;
                            currentSpareBox++;
                            if (currentSpareBox >= spareBoxes.size()) {
                                spareBoxes.add(new String[10][5]);
                            }
                        }
                    }
                }
            }
        }

        // 打印主刀具盒
        System.out.println("主刀具盒:");
        for (int i = 0; i < mainBoxes.size(); i++) {
            System.out.println("主刀具盒 " + (i + 1) + ":");
            printBox(mainBoxes.get(i));
        }

        // 打印备用刀具盒
        System.out.println("备用刀具盒:");
        for (int i = 0; i < spareBoxes.size(); i++) {
            System.out.println("备用刀具盒 " + (i + 1) + ":");
            printBox(spareBoxes.get(i));
        }
    }

    private static void printBox(String[][] box) {
        for (int row = 0; row < 10; row++) {
            for (int col = 0; col < 5; col++) {
                System.out.print((box[row][col] != null ? box[row][col] : "空") + "\t");
            }
            System.out.println();
        }
        System.out.println();
    }
}
